package ar.edu.itba.it.paw.web.validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import ar.edu.itba.it.paw.domain.Auction;
import ar.edu.itba.it.paw.web.command.EnterOfferForm;

@Component
public class EnterOfferFormValidator implements Validator{

	@Override
	public boolean supports(Class<?> clazz) {
		return EnterOfferForm.class.equals(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		EnterOfferForm form = (EnterOfferForm) target;
		
		try{
			if(Float.valueOf(form.getPrice()) <= 0){
				errors.rejectValue("price", "invalid");
			}
			else if(Float.valueOf(form.getPrice()) <= ((Auction)form.getProduct()).getPrice()){
				errors.rejectValue("price", "too_low");
			}
		}catch(Exception e){
			errors.rejectValue("price", "invalid");
		}
	}
}
